package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.business.domain.Item;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/business/statementItem")
public class StatementItemController {
    //模板前缀
    private static final String prefix = "business/statementItem/";

    @Autowired
    private IStatementService statementService;
    @Autowired
    private IStatementItemService statementItemService;

    @RequestMapping("/itemDetail")
    public String itemDetail(Model model,Long statementId){
        //点击明细传入的id查询statement的数据
        Statement statement = statementService.get(statementId);
        //将查询出来的数据放到作用域中
        model.addAttribute("statement",statement);
        //根据数据的状态判断跳转的页面
        if (Statement.STATUS_CONSUME.equals(statement.getStatus())){
            return prefix+"consumeDetail";
        }else {
            return prefix+"payDetail";
        }
    }

    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(StatementItemQuery query,Long statementId){
        //查询服务清单中的服务列表出来并返回出去给bootstrap-table
        return statementItemService.query(query,statementId);
    }

/*    @RequestMapping("/saveItem")
    @ResponseBody
    public AjaxResult saveItem(@RequestBody List<StatementItem>list){
        statementItemService.saveItem(list);
        return AjaxResult.success();
    }*/
    @RequestMapping("/saveItem")
    @ResponseBody
    public AjaxResult saveItem(@RequestBody Item item){
        statementItemService.saveItem1(item);
        return AjaxResult.success();
    }


    @RequestMapping("/pay")
    @ResponseBody
    public AjaxResult pay(Long statementId){
        statementItemService.pay(statementId);
        return AjaxResult.success();
    }

    @RequestMapping("/pay1")
    @ResponseBody
    public AjaxResult pay(@RequestBody Item item){
        statementItemService.pay1(item);
        return AjaxResult.success();
    }

    @RequestMapping("/exportExcel")
    @ResponseBody
    public void exportExcel(HttpServletResponse response,Long statementId) throws IOException {
        response.setHeader("Content-Disposition","attachment;filename=bill.xlsx");
        Workbook is = statementItemService.export(statementId);
        is.write(response.getOutputStream());
    }
}
